<?
class SiteComponent extends Object
{
	var $controller = true;
	var $catsarray = null;
  var $sortfields = array(
	'title'=>'Post.title',
	'status'=>'Post.closed',
	'replies'=>'Post.replies',
	'created'=>'Post.created',
	'added'=>'Comment.created',
	'votes'=>'Comment.vote',
	'value'=>'Vote.value',
	'votedon'=>'Vote.created',
	'modified'=>'Post.modified',
	'rating'=>'Post.points/Post.rated',
	'author'=>'User.login',
	'points'=>'User.vote',
	'topics'=>'User.postcount',
	'comments'=>'User.comcount',
	'regdate'=>'User.created',
	'group'=>'Category.name',
	);

    function startup(&$controller)
    {
      // This method takes a reference to the controller which is loading it.
      // Perform controller initialization here.
			$this->controller =& $controller;
    }

    function getPath($cid)
    {
      $catmodel =& new Category();
			$this->catsarray = $catmodel->findAll(null,array("Category.id", "Category.parent_id", "Category.name", "Category.description"),null,null,null,-1);
			$this->controller->set('categorypath', $this->_getpatha($cid));
    }

    function _getpatha($cid) {
	   // look up the parent of this node
	   foreach ($this->catsarray as $cata)
	   	if ($cata['Category']['id']==$cid)
			$row=$cata;

   		// save the path in this array
	   $path = array();

   	// only continue if this $node isn't the root node
   	// (that's the node with no parent)
   	if (isset($row['Category']['parent_id'])) {
       // the last part of the path to $node, is the name
       // of the parent of $node
       $path[] = $row['Category'];

       // we should add the path to the parent of this node
       // to the path
       $path = array_merge($this->_getpatha($row['Category']['parent_id']), $path);
   }

   		// return the path
   		return $path;
	}

	function getCalendar($y=null,$m=null) {
		$p=array();
		if(!$y && !$m) {
			$y=date("Y");
			$m=date("m");
		}
		$date=sprintf("%04d",$y)."-".sprintf("%02d",$m);
		$pm =& new Post();
		$pda=$pm->findAll("Post.created LIKE '{$date}%'",array("DISTINCT DAYOFMONTH(Post.created) AS postday"),"postday ASC",null,null,-1);
		foreach ($pda as $pd) $p[$pd[0]['postday']]=$pd[0]['postday'];
		$this->controller->set('postdays', $p);
	}

	function getSort($dsort='created') {
    if(isset($this->controller->params['url']['sort']) && in_array($this->controller->params['url']['sort'],array_keys($this->sortfields)))
				$sort=$this->sortfields[$this->controller->params['url']['sort']];
		else
				$sort=$this->sortfields[$dsort];

		if(isset($this->controller->params['url']['order']) && ($this->controller->params['url']['order']=="asc" || $this->controller->params['url']['order']=="desc"))
				$order=$this->controller->params['url']['order'];
		else
				$order="desc";

			$flipsort=array_flip($this->sortfields);
			$this->controller->set('sortfield', $flipsort[$sort]);
			$this->controller->set('sortorder', $order);
			$this->controller->set('nextsortorder', ($order=='asc')? "desc":"asc");

			return array($sort,$order);
	}

}
?>